from browser import document
from browser import html
from browser import ajax
from browser import confirm
from browser import alert
import json
import time

userManageBtn = document["userManageBtn"]
groupManageBtn = document["groupManageBtn"]
hostsManageBtn = document["hostsManageBtn"]

workWindow = document["workWindow"]

userTableTitle = ["Id","Username","CreateDate","UpdateDate","GroupIDs","Remask","Opeation"]


def whiteTitle(titleName):
    thead = html.THEAD()
    tr = html.TR()

    for i in range(len(titleName)):
        th = html.TH(userTableTitle[i])
        tr <= th

    thead <= tr
    return thead

def deluserComplete(req):
    alert(req.text)
    showUserInfos()

def delUsers(ev):
    btn = ev.target
    username = btn.dataset["Username"]
    userid = btn.dataset["userid"]

    if confirm("确定删除用户 "+username+" 信息么?"):
        data = {
            "deluserid": userid
        }

        request = ajax.Ajax()
        request.bind("complete", deluserComplete)
        request.open("POST", "/manageUsers", False)
        request.send(data)

def showUsersEv(req):
    if req.status == 200:
        data = json.loads(req.text)

        if 0 == data["Code"]:
            if None == data["Data"]:
                return

            tbody = html.TBODY()
            for line in data["Data"]:

                tr = html.TR()

                for k in userTableTitle:
                    if k in line:
                        if k == "Id":
                            td = html.TH(line[k])

                        elif k == "CreateDate" or k == "UpdateDate":
                            td = html.TD(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(line[k]))))

                        else:
                            td = html.TD(line[k])

                    elif k == "Opeation":
                        UpdateA = html.A()
                        UpdateA.href = "./updateUsers.html?username=" + line["Username"]

                        UpdateBtn = html.BUTTON("update")
                        UpdateBtn.className = "btn btn-cyan btn-round btn-xs"

                        UpdateA <= UpdateBtn

                        DelBtn = html.BUTTON("delete")
                        DelBtn.className = "btn btn-danger btn-round btn-xs"
                        DelBtn.dataset["Username"] = line["Username"]
                        DelBtn.dataset["userid"] = line["Id"]

                        DelBtn.bind("click", delUsers)

                        td = html.TD(UpdateA+" "+DelBtn)

                    tr <= td

                tbody <= tr
                userTable <= tbody

def showUserInfos():
    workWindow.clear()

    addUserA = html.A()
    addUserA.href = "./addUsers.html"
    # addUserA.target = "_blank"

    addUserBtn = html.BUTTON("AddUser")
    addUserBtn.type = "button"
    addUserBtn.className = "btn btn-primary btn-sm"

    addUserA <= addUserBtn


    workWindow <= html.DIV(addUserA)

    global userTable
    userTable = html.TABLE()
    userTable.className = "table table-hover"

    userTable <= whiteTitle(userTableTitle)
    workWindow <= userTable

    request = ajax.Ajax()
    request.bind("complete",showUsersEv)
    request.open("GET","/showUsers",False)
    request.send()

def userManage(ev):
    showUserInfos()


userManageBtn.bind("click",userManage)